!define PRODUCT_NAME "notewhal"
!define PRODUCT_PUBLISHER "http://code.google.com/u/beinghuman.ng/"
!define PRODUCT_WEB_SITE "http://notewhal.googlecode.com"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\notewhal.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"

SetCompressor lzma

; MUI 1.67 compatible ------
!include "MUI2.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "res\win-install.ico"
!define MUI_UNICON "res\win-uninstall.ico"

; MUI Settings / Header
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_RIGHT
!define MUI_HEADERIMAGE_BITMAP "res\header-r.bmp"
 
; MUI Settings / Wizard
!define MUI_WELCOMEFINISHPAGE_BITMAP "res\wizard.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "res\wizard.bmp"

BrandingText "${PRODUCT_NAME} ${PRODUCT_VERSION}"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "license.txt"
; Components page
;!insertmacro MUI_PAGE_COMPONENTS
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Start menu page
var ICONS_GROUP
!define MUI_STARTMENUPAGE_NODISABLE
!define MUI_STARTMENUPAGE_DEFAULTFOLDER "notewhal"
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}"
!insertmacro MUI_PAGE_STARTMENU Application $ICONS_GROUP
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!define MUI_FINISHPAGE_RUN "$INSTDIR\notewhal.exe"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; Reserve files
;!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "${PRODUCT_NAME}-${PRODUCT_VERSION}_win32_setup.exe"
InstallDir "$PROGRAMFILES\notewhal"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

Function .onInit
	;detect process
	FindProcDLL::FindProc "${PRODUCT_NAME}.exe"
	IntCmp $R0 1 0 notRunning
		MessageBox MB_OK|MB_ICONEXCLAMATION "${PRODUCT_NAME} is running. Please close it first." /SD IDOK
		Abort
	notRunning:
	
	  ReadRegStr $R0 HKLM \
	  "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" \
	  "UninstallString"
	  StrCmp $R0 "" done
	 
	  MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
	  "${PRODUCT_NAME} is already installed. $\n$\nClick `OK` to remove the \
	  previous version or `Cancel` to cancel this upgrade." \
	  IDOK uninst
	  Abort
	 
	;Run the uninstaller
	uninst:
	  ClearErrors
	  ExecWait '$R0 _?=$INSTDIR' ;Do not copy the uninstaller to a temp file
	 
	  IfErrors no_remove_uninstaller done
		;You can either use Delete /REBOOTOK in the uninstaller or add some code
		;here to remove the uninstaller. Use a registry key to check
		;whether the user has chosen to uninstall. If you are using an uninstaller
		;components page, make sure all sections are uninstalled.
	  no_remove_uninstaller:
	 
	done:

FunctionEnd

Section "core" SEC01
  SetShellVarContext all
  SetOverwrite ifnewer
  SetOutPath "$INSTDIR\data"
  File "data\*.*"
  SetOutPath "$INSTDIR"
  File "dist\notewhal\*.*"
  ;File "dist\notewhal.exe"
  ;File "dist\notewhal.chm"
  ;File "dist\license.txt"

; Shortcuts
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  CreateDirectory "$SMPROGRAMS\$ICONS_GROUP"
  CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\notewhal.lnk" "$INSTDIR\notewhal.exe" "" "$INSTDIR\data\icon.ico" 0
  CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\notewhal help.lnk" "$INSTDIR\notewhal.chm" "" "" 0
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section -AdditionalIcons
  SetShellVarContext all
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\Uninstall.lnk" "$INSTDIR\uninst.exe"
  CreateShortCut "$SMSTARTUP\notewhal.lnk" "$INSTDIR\notewhal.exe" "" "$INSTDIR\data\icon.ico" 0
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\notewhal.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\notewhal.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit

	;detect process
	FindProcDLL::FindProc "${PRODUCT_NAME}.exe"
	IntCmp $R0 1 0 notRunning
		MessageBox MB_OK|MB_ICONEXCLAMATION "${PRODUCT_NAME} is running. Please close it first." /SD IDOK
		Abort
	notRunning:

  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  SetShellVarContext all
  !insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP
  Delete "$INSTDIR\*.*"
  ;Delete "$INSTDIR\${PRODUCT_NAME}.url"
  ;Delete "$INSTDIR\uninst.exe"
  ;Delete "$INSTDIR\notewhal.exe"
  ;Delete "$INSTDIR\license.txt"
  ;Delete "$INSTDIR\notewhal.chm"
  Delete "$INSTDIR\data\*.*"

  Delete "$SMPROGRAMS\$ICONS_GROUP\Uninstall.lnk"
  Delete "$SMPROGRAMS\$ICONS_GROUP\notewhal.lnk"
  Delete "$SMPROGRAMS\$ICONS_GROUP\notewhal help.lnk"
  Delete "$SMSTARTUP\notewhal.lnk"

  RMDir "$INSTDIR\data"
  RMDir "$SMPROGRAMS\$ICONS_GROUP"
  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  SetAutoClose true
SectionEnd